#include "vgsvr.h"

int vgs_init(int *vgs_fd)
{

    char   *hostname   =   "localhost";
    char   *password   =   "changeme";
    int     port        =   3219;

    int     listen_socket;
    int     accept_socket;

    struct sockaddr_in  socket_addr;
    struct hostent     *host;

    if ((listen_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("socket() failed");
        return 1;
    }

    memset((void *)&socket_addr, 0, sizeof(socket_addr));
    socket_addr.sin_family = AF_INET;
    socket_addr.sin_port = htons(port);
    if ((host = gethostbyname(hostname)) == NULL) {
        perror("gethostbyname() failed");
        return 1;
    }
    memcpy((void *)&socket_addr.sin_addr, host->h_addr, (size_t)host->h_length);

    if (bind(listen_socket, (struct sockaddr *)&socket_addr, sizeof(socket_addr)) == -1) {
        perror("bind() failed");
        return 1;
    }

    if (listen(listen_socket, 5) == -1 ) {
        perror("listen() failed");
        return 1;
    }

    struct sockaddr_in  from;

    int         tmp;
    int         data_fd;
    socklen_t   socket_len;

    char        buf[VGS_BUF_SIZE];

    socket_len = (socklen_t)sizeof(socket_addr);

    if ((accept_socket = accept(listen_socket, (struct sockaddr *)&socket_addr, &socket_len)) != -1) {
        close( listen_socket );

        vgdb_server_get_gvim_init_response();
        server->gvim_socket = accept_socket;
        return 0;
    } else {
        return 1;
    }

}




        line_ptr = server->line_buff;

        length = recv(accept_socket, (void*)buf, VGS_BUF_SIZE, 0);
        buf[tmp-1] = '\0';
        handle_response(buf);
        tmp = recv(accept_socket, (void*)buf, VGS_BUF_SIZE, 0);
                buf[tmp-1] = '\0';
        handle_response(buf);
        while(1){

            tmp = recv(accept_socket, (void*)buf, VGS_BUF_SIZE, 0);
            if(tmp == VGS_BUF_SIZE){
                handle_long_response();
            }else if(tmp==0){
                printf("Vim exit normally\n");
                break;
            }else{
                buf[tmp-1] = '\0';
                handle_response(buf);
            }
            /*
            printf("VGS:INPUT:");
            fflush(stdout);
            fgets(buf, VGS_BUF_SIZE, stdin);
            printf("VGS:INPUT-GET:%s",buf);
            fflush(stdout);
            if(strlen(buf)<3){
                printf("VGS:NOT-SEND\n");
                fflush(stdout);
            }else{
                printf("VGS:SEND:%s",buf);
                fflush(stdout);
                send(accept_socket, (void*)buf, strlen(buf), 0);
            }
            */
        }
    } else if (errno != EINTR && errno != EWOULDBLOCK && errno != EAGAIN) {
        perror("accept() failed");
    }
    close( accept_socket );
    return 0;
}






int main(int argc, char* argv[] ){
    int     vgs_fd;

    //if( argc 
    if(vgs_init(&vgs_fd)){
        fprintf(stderr, "VGdb init failed\n");
        fflush(stderr);
        return 1;
    }else{
        return vgs_run(vgs_fd);
    }
}
